﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TestEffectiveCS
{
    class Test11
    {
        public class Person
        {
            public Person(string fName, string lName)
            {
                this.firstName = fName;
                this.lastName = lName;
            }

            public string firstName;
            public string lastName;
        }

        public class People : IEnumerable
        {
            private Person[] _people;
            public People(Person[] pArray)
            {
                _people = new Person[pArray.Length];

                for (int i = 0; i < pArray.Length; i++)
                {
                    _people[i] = pArray[i];
                }
            }

            public IEnumerator GetEnumerator()
            {
                return new PeopleEnum(_people);
            }
        }

        public class PeopleEnum : IEnumerator
        {
            public Person[] _people;

            // Enumerators are positioned before the first element
            // until the first MoveNext() call.
            int position = -1;

            public PeopleEnum(Person[] list)
            {
                _people = list;
            }

            public bool MoveNext()
            {
                position++;
                return (position < _people.Length);
            }

            public void Reset()
            {
                position = -1;
            }

            public object Current
            {
                get
                {
                    try
                    {
                        return _people[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }

        //实现枚举接口：IEnumerable和IEnumerator
        void TestPerson()
        {
            Person[] peopleArray = new Person[3]
            {
                new Person("John", "Smith"),
                new Person("Jim", "Johnson"),
                new Person("Sue", "Rabon"),
            };

            People peopleList = new People(peopleArray);
            foreach (Person p in peopleList)
                Console.Write(p.firstName + " " + p.lastName + "\n");

            IEnumerator person = peopleList.GetEnumerator();
            while (person.MoveNext())
            {
                Person p = person.Current as Person;
                Console.Write(p.firstName + " " + p.lastName + "\n");
            }
        }

        //Foreach遍历2维数组
        void TestSquare()
        {
            int[,] frame = new int[10, 10];
            foreach (var item in frame) Console.Write(item + "\t");
        }


        public Test11()
        {
            //TestSquare();
            TestPerson();
        }
    }
}
